<template>
  <div class="todo-container">
    <div class="head">
      <span>TodoList</span>
    </div>
    <div class="todo-wrap">
      <Header @getTitle="addTodo"></Header>
      <List :todos="todos" :removeTodo="removeTodo" :updateTodo="updateTodo"  :isDone="isDone"></List>
      <Footer :todos="todos" :doneCount="doneCount" @allDone="allDone" :clearAllDone="clearAllDone" :doneAll="doneAll"></Footer>
      <div v-show="showupdate" class="changebox">
        <input class="update" type="text" name="" placeholder="请输入需要修改的内容"  v-model="msg" ><button class="sumitbtn" @click="update">提交</button>
      </div>
    </div>
    <div class="copyRight">
      <span>19软件2班李锵  88</span> 
    </div>
  </div>
</template>

<script>
import Header from '@/components/Header.vue'
import List from '@/components/Lists.vue'
import Footer from '@/components/Footer.vue'
import axios from 'axios'

export default {
  name: 'App',
  data () {
    return {
      msg:'',
      showupdate:false,
      todos: [],
      newData: {},
      indexid:''
    }
  },
  methods: {
    async addTodo (val) {
      await axios.post('http://175.24.200.41:3000/add',val)
      this.todos.unshift(val)
    },
    async removeTodo (val) {
      const num = this.todos.findIndex(item => item.id === val)
      this.todos.splice(num, 1)
      await axios.post('http://175.24.200.41:3000/delete',{id:val})
    },
    async isDone (check, val) {
      const num = this.todos.findIndex(item => item.id === val)
      this.todos[num].done = check
      let obj={check,val}
      await axios.post('http://175.24.200.41:3000/updatedone',obj)
      // console.log(val,check)
    },
    async updateTodo (val) {
      this.indexid=val
      this.showupdate=true
      // const num = this.todos.findIndex(item => item.id === val)
      // this.todos.splice(num, 1)
      // await axios.post('http://localhost:3000/delete',{id:val})
    },
    async allDone (val) {
      await axios.post('http://175.24.200.41:3000/alldone')
      if (val) {
        this.todos.forEach(item => {
          item.done = true
        })
      } else {
        this.todos.forEach(item => {
          item.done = false
        })
      }
    },
    async clearAllDone () {
      this.todos = this.todos.filter(item => !item.done)
      await axios.post('http://175.24.200.41:3000/clearall')
    },
    async showlist(){
      let val= await axios.get('http://175.24.200.41:3000/show')
      this.todos=val.data.data
    },
    async update(){
        let id=this.indexid
        let nowmsg =this.msg
        let obj={id,nowmsg}
        await axios.post('http://175.24.200.41:3000/update',obj)
        this.showupdate=false
        this.msg=''
        this.showlist()
    }
  },
  computed: {
    doneCount () {
      return this.todos.filter(item => item.done).length
    },
    doneAll () {
      if (this.todos.length === 0 || this.todos.some(item => !item.done)) {
        return false
      }
      return true
    }
  },
  components: {
    Header,
    List,
    Footer
  },
  created () {
    this.showlist()
    var data = localStorage.getItem('todo')
    if (data != null) {
      this.todos = JSON.parse(data)
    }
  },
  // updated ( val) {
    
  // },
  watch: {
    todos: {
      deep: true,
      handler (val) {
        localStorage.setItem('todo', JSON.stringify(val))
      }
    }
  }
}
</script>

<style lang="less">
.todo-container {
  margin: 0 auto;
  .head{
    margin: 0 auto;
    height: 50px;
    background-color: rgba(0, 0, 0, 0.507);
    color: white;
    text-align: center;
    line-height: 50px;
    font-size: 30px;
  }
  .todo-wrap{
    background-color: #fff;
    width: 600px;
    padding: 10px;
    margin: 10px auto;
    border: 1px solid #ddd;
    border-radius: 5px;
  }
  .copyRight{
    text-align: center;
    color: #aaa;
  }

  .changebox{
    margin-top: 30px;
    display: flex;
    justify-content:space-between ;
  }
  .update{
    font-size: 100%;
    border: #c0c0c0 0.5px solid;
    outline: none;
  }
  .sumitbtn{
    position: relative;
    bottom: 1px;
    width: 80px;
    color: #444444;
    background: #F3F3F3;
    border: 1px #DADADA solid;
    padding: 5px 10px;
    border-radius: 2px;
    font-weight: bold;
    font-size: 9pt;
    outline: none;
  }
}
</style>
